package com.sky.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.sky.dto.DataOverViewQueryDTO;
import com.sky.entity.BusinessData;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "数据统计相关接口")
@RequestMapping("/admin/report")
public class ReportController {
    @Autowired
    private ReportService reportService;

    /**
     * 营业额统计
     * @param dataOverViewQueryDTO
     * @return
     */
    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额统计接口")
    public Result<TurnoverReportVO> getTurnover(DataOverViewQueryDTO dataOverViewQueryDTO){
        TurnoverReportVO turnoverReportVO = reportService.getTurnover(dataOverViewQueryDTO);
        return Result.success(turnoverReportVO);
    }

    /**
     * 用户统计
     * @param dataOverViewQueryDTO
     * @return
     */
    @GetMapping("/userStatistics")
    @ApiOperation("用户统计接口")
    public Result<UserReportVO> getUser(DataOverViewQueryDTO dataOverViewQueryDTO){
        UserReportVO userReportVO = reportService.getUser(dataOverViewQueryDTO);
        return Result.success(userReportVO);
    }

    /**
     * 订单统计
     * @param dataOverViewQueryDTO
     * @return
     */
    @GetMapping("/ordersStatistics")
    @ApiOperation("订单统计接口")
    public Result<OrderReportVO> getOrders(DataOverViewQueryDTO dataOverViewQueryDTO){
        OrderReportVO orderReportVO = reportService.getOrders(dataOverViewQueryDTO);
        return Result.success(orderReportVO);
    }

    /**
     * 查询销量排名top10
     * @param dataOverViewQueryDTO
     * @return
     */
    @GetMapping("/top10")
    @ApiOperation("查询销量排名top10接口")
    public Result<SalesTop10ReportVO> salesTop10(DataOverViewQueryDTO dataOverViewQueryDTO){
        SalesTop10ReportVO salesTop10ReportVO = reportService.salesTop10(dataOverViewQueryDTO);
        return Result.success(salesTop10ReportVO);
    }

    /**
     * 导出订单
     * @param response
     * @return
     */
    @GetMapping("/export")
    @ApiOperation("导出订单接口")
    public void export(HttpServletResponse response) throws IOException {
        List<BusinessData> list = reportService.export();
        //设置响应格式
        response.setContentType("application/vnd.ms-excel");
        //设置字符集
        response.setCharacterEncoding("utf-8");
        EasyExcel.write(response.getOutputStream(), BusinessData.class)
                //sheet名字
                .sheet()
                // 设置字段宽度为自动调整，不太精确
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(list);
    }
}
